package com.customization.huangwm.workflowNodeAction.SW003;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.customization.huangwm.dao.SW003.LoggingDao;
import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.HttpRequest;
import com.customization.huangwm.utils.LogTool;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Huangwm
 * @description 物料歸還流程 提交時校驗數量
 * 歸檔時返還數量,並且更新記錄狀態，對接用友返回對應物料數量
 */
public class AssetsReturn implements Action {

    private LogTool log = new LogTool("/log/SW003/huangwm/WfAction/AssetsReturn", false);
    private DBTools dbTools = new DBTools();
    private DateTools dateTools = new DateTools();
    public String type;//submit_create,submit_review,submit_end

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String nodeType = requestInfo.getRequestManager().getNodetype();
        String src = requestInfo.getRequestManager().getSrc();
        int creater = requestInfo.getRequestManager().getCreater();
        log.writeLog("物料歸還流程>>>" + requestid + ">>" + nodeType + ">>" + src + ">>" + type);
        if (type.equals("") || (!type.equals("submit_create") && !type.equals("submit_review") && !type.equals("submit_end") && !type.equals("submit_reject"))) {
            requestInfo.getRequestManager().setMessageid(requestid);
            requestInfo.getRequestManager().setMessagecontent("未獲取到Action參數type或參數值不正確,請聯繫管理員檢查後台接口動作設置!;");
            return FAILURE_AND_CONTINUE;
        }

        RecordSet rs1 = new RecordSet();
        String tableName = requestInfo.getRequestManager().getBillTableName();
        String billid = requestInfo.getRequestManager().getBillid() + "";
        Map<String,String> main = new HashMap<>();
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        for (Property property : properties){
            main.put(property.getName().toLowerCase(),property.getValue());
        }
        String operator = main.getOrDefault("applicationuser",creater+"");//申请人
        String manageDep = main.get("managedep");//主管理部門
        String cwarehouseid = "02";
        if(!manageDep.equals("")){
            cwarehouseid = dbTools.getWarehouse(manageDep);
        }

//        RecordSetTrans rst = new RecordSetTrans();
//        rst1.setAutoCommit(false);

        RecordSetTrans rst2 = new RecordSetTrans();
        rst2.setAutoCommit(false);

        if (src.equals("reject")) {
            try {
                String sql1 = "update uf_003_applicationRecord set quantityInReturn=isnull(quantityInReturn,0)-dt.applicationQuantity " +
                        "\n from " + tableName + "_dt1 dt " +
                        "\n where uf_003_applicationRecord.id=dt.recordid and dt.mainid=" + billid;
                if (rst2.getDBType().equals("oracle")) {
                    sql1 = "update uf_003_applicationRecord set quantityInReturn=NVL(quantityInReturn,0)-(select applicationQuantity from " + tableName + "_dt1 where uf_003_applicationRecord.id=recordid and mainid=" + billid + ")" +
                            "\n where id in (select recordid from " + tableName + "_dt1 where mainid=" + billid + ")";
                }
                log.writeLog("退回釋放預計歸還數量SQL：" + sql1);
                boolean result = rst2.execute(sql1);
                if (!result) {
                    rst2.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("退回釋放預歸還數量失敗,執行SQL異常;\n" + sql1);
                    return FAILURE_AND_CONTINUE;
                } else {
                    rst2.commit();
                }
            } catch (Exception e) {
                rst2.rollback();
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("退回釋放預還數量失敗,程序異常;\n" + e.getMessage());
                return FAILURE_AND_CONTINUE;
            }
        } else if (type.equals("submit_create")) {
            try {
                String sql1 = "select dt.applicationQuantity as ghsl,rc.applicationQuantity as jlsl,(rc.applicationQuantity-dt.applicationQuantity) as chae " +
                        "\n from " + tableName + "_dt1 dt left join uf_003_applicationRecord rc " +
                        "\n on dt.recordid=rc.id where mainid=" + billid;
                String sql2 = "update uf_003_applicationRecord set quantityInReturn=isnull(quantityInReturn,0)+dt.applicationQuantity " +
                        "\n from " + tableName + "_dt1 dt " +
                        "\n where uf_003_applicationRecord.id=dt.recordid and dt.mainid=" + billid;
                if (rs1.getDBType().equals("oracle")) {
                    sql2 = "update uf_003_applicationRecord set quantityInReturn=NVL(quantityInReturn,0)+(select applicationQuantity from " + tableName + "_dt1 where uf_003_applicationRecord.id=recordid and mainid=" + billid + ")" +
                            "\n where id in (select recordid from " + tableName + "_dt1 where mainid=" + billid + ")";
                }
                log.writeLog("查詢校驗數量SQL：" + sql1);
                log.writeLog("創建後預歸還數量SQL：" + sql2);
                rs1.execute(sql1);
                int i = 1;
                while (rs1.next()) {
                    Double chae = rs1.getDouble("chae");
                    Double ghsl = rs1.getDouble("ghsl");
                    Double jlsl = rs1.getDouble("jlsl");
                    if (chae < 0) {
                        requestInfo.getRequestManager().setMessageid(requestid);
                        requestInfo.getRequestManager().setMessagecontent("第" + i + "行歸還數量[" + ghsl + "]超過申請記錄中的數量[" + jlsl + "]");
                        return FAILURE_AND_CONTINUE;
                    }
                    i++;
                }
//                RecordSetTrans rst2 = new RecordSetTrans();
//                rst2.setAutoCommit(false);
                boolean result = rst2.execute(sql2);
                if (!result) {
                    rst2.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("創建後預歸還數量失敗,執行SQL異常;\n" + sql2);
                    return FAILURE_AND_CONTINUE;
                } else {
                    rst2.commit();
                }
            } catch (Exception e) {
                rst2.rollback();
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("創建提交檢查/預還數量失敗,程序異常;\n" + e.getMessage());
                return FAILURE_AND_CONTINUE;
            }

        } else if (type.equals("submit_end")) {

            if(!main.containsKey("lastoperator")){
                rst2.rollback();
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("未獲取到最後審核人字段lastoperator");
                return FAILURE_AND_CONTINUE;
            }
            String lastoperator = main.get("lastoperator");
            if( lastoperator.equals("") ){
                lastoperator = dbTools.getCurrentoperatorFromNodeName(tableName,requestid,"管理部門審核");
            }
            if(lastoperator.equals("") ){
                rst2.rollback();
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("未獲取到最後審核人字段值");
                return FAILURE_AND_CONTINUE;
            }

            try {
                RecordSet rs2 = new RecordSet();
                //歸檔時釋放申請記錄數量
                String sql1 = "update uf_003_applicationRecord set returnQuantity=isnull(returnQuantity,0)+dt.applicationQuantity, " +
                        "\n quantityInReturn=quantityInReturn-dt.applicationQuantity from " + tableName + "_dt1 dt " +
                        "\n where uf_003_applicationRecord.id=dt.recordid and dt.mainid=" + billid;
                //歸檔時釋放物料表申請數量
                String sql2 = "update uf_003_AssteInfo set usedQuantity=usedQuantity-dt.applicationQuantity,InventoryQuantity=InventoryQuantity+dt.applicationQuantity " +
                        "\n from " + tableName + "_dt1 dt " +
                        "\n where uf_003_AssteInfo.id=dt.assetCode and dt.mainid=" + billid;
                //歸檔時更新資產表狀態為0→A  这里还需要判断下不为报废状态
                String sql3 = "update uf_003_AssetInfo2 set assetStatus='Stock' from " + tableName + "_dt1 dt " +
                        "\n where uf_003_AssetInfo2.id=dt.mainAssetCode and assetStatus<>'Inactive' and dt.mainid=" + billid;
                if (rs2.getDBType().equals("oracle")) {
                    sql1 = "update uf_003_applicationRecord set quantityInReturn=NVL(quantityInReturn,0)-(select applicationQuantity from " + tableName + "_dt1 where uf_003_applicationRecord.id=recordid and mainid=" + billid + ")," +
                            "\n returnQuantity=NVL(returnQuantity,0)+(select applicationQuantity from " + tableName + "_dt1 where uf_003_applicationRecord.id=recordid and mainid=" + billid + ")" +
                            "\n where id in (select recordid from " + tableName + "_dt1 where mainid=" + billid + ")";
                    //sql2 = "update uf_003_AssteInfo set usedQuantity=usedQuantity-(select applicationQuantity from " + tableName + "_dt1 where assetCode=uf_003_AssteInfo.id and mainid=" + billid + ")" +
                    //        "\n where id in(select assetCode from  " + tableName + "_dt1 where mainid=" + billid + ")";
                    sql2 = "update uf_003_AssteInfo set usedQuantity=usedQuantity-(select applicationQuantity  from (\n" +
                            "select assetCode,sum(applicationQuantity )as applicationQuantity ,mainid from  "+ tableName +"_dt1 group by assetCode,mainid) t where t.assetCode=uf_003_AssteInfo.id  and t.mainid="+billid+" )\n" +
                            ",InventoryQuantity=InventoryQuantity+(select applicationQuantity  from (\n" +
                            " select assetCode,sum(applicationQuantity )as applicationQuantity ,mainid from  "+ tableName +"_dt1 group by assetCode,mainid) t where t.assetCode=uf_003_AssteInfo.id  and t.mainid="+billid+" )\n" +
                            " where id in( select assetCode from  "+ tableName +"_dt1 where mainid="+billid+" )";
                    sql3 = "update uf_003_AssetInfo2 set assetStatus=(select 'Stock' from " + tableName + "_dt1 dt where  uf_003_AssetInfo2.id=dt.mainAssetCode and dt.mainid=" + billid + ")" +
                            "\n where id in(select mainAssetCode from  " + tableName + "_dt1 where mainid=" + billid + ") and assetStatus<>'Inactive'";
                }
                log.writeLog("歸檔時釋放申請記錄數量SQL：" + sql1);
                log.writeLog("歸檔時釋放物料表申請數量SQL：" + sql2);
                log.writeLog("歸檔時更新資產表狀態SQL：" + sql3);

                //20221019 歸還后增加倉庫庫存數量
                String sql4 = "select * from "+tableName+"_dt1 where mainid="+billid;
                RecordSet checkWarehouse = new RecordSet();
                RecordSet newRs = new RecordSet();
                checkWarehouse.execute(sql4);
                while (checkWarehouse.next()){
                    String assetCode = checkWarehouse.getString("assetCode");
                    Map<String,String> assetInfo = dbTools.getAssetAllFieldoByID(rs2,assetCode);

                    Double applicationQuantity = checkWarehouse.getDouble("applicationQuantity");
                    String wareHouse = checkWarehouse.getString("field1");
                    if(wareHouse.isEmpty()){
                        requestInfo.getRequestManager().setMessageid(requestid);
                        requestInfo.getRequestManager().setMessagecontent("物料編碼["+assetInfo.get("assetcode")+"]歸還更新倉庫庫存失敗--未獲取到表單倉庫字段值");
                        return FAILURE_AND_CONTINUE;
                    }

                    //20230719 增加归还判断仓库是新增还是插入-------------start
                    String recordid = checkWarehouse.getString("recordid");//申请记录id
                    String field1 = checkWarehouse.getString("field1");//归还仓库
                    String outfield1 = checkWarehouse.getString("outfield1");//借出仓库
                    String mainAssetCode = checkWarehouse.getString("mainAssetCode");//关联资产
                    if(!field1.equals(outfield1)){
                        //归还仓库与借出仓库不同 将数量更新到归还仓库
                        newRs.execute("select id from uf_003_WarehouseNum where assetCode="+assetCode+" and wareHouse="+field1);
                        //存在记录 减少借出仓库的库存(总库存,已出库减少即可 剩余库存由于已经扣减不用再减少
                        if(newRs.next()){
                            //num1 總庫存 num2 已出庫 num3 剩餘庫存
                            String s1 = "update uf_003_WarehouseNum set num1=NVL(num1,0)-"+applicationQuantity+", num2=NVL(num2,0)-"+applicationQuantity+" where assetCode="+assetCode+" and wareHouse="+outfield1;
                            log.writeLog("歸還時减少借出仓库的库存結果："+s1);
                            boolean thisCheck1 = rst2.execute(s1);
                            log.writeLog("歸還時减少借出仓库的库存SQL："+thisCheck1);
                            if(!thisCheck1){
                                rst2.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("物料編碼["+assetInfo.get("assetcode")+"]歸還更新倉庫庫存失敗");
                                return FAILURE_AND_CONTINUE;
                            }
                            //增加归还仓库的仓库库存
                            String s2 = "update uf_003_WarehouseNum set num1=NVL(num1,0)+"+applicationQuantity+", num3=NVL(num3,0)+"+applicationQuantity+" where assetCode="+assetCode+" and wareHouse="+field1;
                            boolean thisCheck2 = rst2.execute(s2);
                            log.writeLog("歸還時增加归还仓库的仓库库存結果："+thisCheck2);
                            log.writeLog("歸還時增加归还仓库的仓库库存SQL："+s2);
                            if(!thisCheck2){
                                rst2.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("物料編碼["+assetInfo.get("assetcode")+"]歸還更新倉庫庫存失敗");
                                return FAILURE_AND_CONTINUE;
                            }

                        }else{
                            //当前物料未找到归还仓库的记录 插入
                            String s = "insert into uf_003_WarehouseNum" +
                                    "(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID"+
                                    ",assetCode,wareHouse,num1,num2,num3,num4) " +
                                    "values(?,?,?,?,?,?,?,?,?,?,?,?)";
                            String modedatacreatedate = dateTools.getCurrentDate();
                            String modedatacreatetime = dateTools.getCurrentTime();
                            String uuid = dateTools.getUUID();
                            String uf_003_WareHouseNum_Modeid = new BaseBean().getPropValue("SW003", "uf_003_WareHouseNum_Modeid");
                            if(uf_003_WareHouseNum_Modeid.isEmpty()){
                                rst2.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("配置文件SW003.peoperties中缺少uf_003_WareHouseNum_Modeid配置項");
                                return FAILURE_AND_CONTINUE;
                            }
                            Object[] objects = new Object[]{
                                    uf_003_WareHouseNum_Modeid, 1, 0, modedatacreatedate, modedatacreatetime, uuid,
                                    assetCode,field1,applicationQuantity,0,applicationQuantity,0
                            };
                            log.writeLog("沒有新倉庫記錄  插入");
                            log.writeLog(JSONObject.toJSONString(objects));
                            boolean result1 = rst2.executeUpdate(s,objects);
                            if(!result1){
                                rst2.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("物料編碼["+assetInfo.get("assetcode")+"]歸還到新仓库["+field1+"]插入数据失败");
                                return FAILURE_AND_CONTINUE;
                            }
                        }

                        //更新關聯資產的倉庫
                        if(!mainAssetCode.isEmpty()){
                            String up3 = "update uf_003_AssetInfo2 set wareHouse="+field1+" where id in("+mainAssetCode+")";
                            log.writeLog("更新關聯資產的倉庫>"+up3);
                            boolean result2 = rst2.executeUpdate(up3);
                            if(!result2){
                                rst2.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("更新關聯資產的倉庫失敗");
                                return FAILURE_AND_CONTINUE;
                            }
                        }

                    }else{
                        //num1 總庫存 num2 已出庫 num3 剩餘庫存
                        log.writeLog("歸還倉庫庫存SQL ：update uf_003_WarehouseNum set num2=NVL(num2,0)-"+applicationQuantity+",num3=NVL(num3,0)+"+applicationQuantity +" where assetCode="+assetCode+" and wareHouse="+field1);
                        boolean thisCheck = rst2.execute("update uf_003_WarehouseNum set num2=NVL(num2,0)-"+applicationQuantity+",num3=NVL(num3,0)+"+applicationQuantity +" where assetCode="+assetCode+" and wareHouse="+field1);
                        log.writeLog("歸還時歸還倉庫庫存結果："+thisCheck);
                        if(!thisCheck){
                            rst2.rollback();
                            requestInfo.getRequestManager().setMessageid(requestid);
                            requestInfo.getRequestManager().setMessagecontent("物料編碼["+assetInfo.get("assetcode")+"]歸還更新倉庫["+wareHouse+"]庫存失敗");
                            return FAILURE_AND_CONTINUE;
                        }
                    }

                }
                //20230719 增加归还判断仓库是新增还是插入-------------end

                boolean result1 = rst2.execute(sql1);
                boolean result2 = rst2.execute(sql2);
                boolean result3 = rst2.execute(sql3);
                log.writeLog("SQL執行結果;result1=" + result1 + ";result2=" + result2 + ";result3=" + result3);

                if (!result1 || !result2 || !result3) {
                    rst2.rollback();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("歸檔更新數量失敗,SQL執行異常;result1=" + result1 + ";result2=" + result2 + ";result3=" + result3);
                    return FAILURE_AND_CONTINUE;
                } else {
                    LoggingDao loggingDao = new LoggingDao();
                    List<Map<String, String>> logList = new ArrayList<>();//日志记录
                    RecordSet rs3 = new RecordSet();

                    //更新申領記錄的歸還狀態
                    rst2.execute(" update uf_003_applicationRecord set oper_staus=1 where applicationQuantity=returnQuantity");

                    //2023年10月17日 根据会议沟通取消出入库传yy
                    //先對接用友 再提交更新，對接失敗則rollback
                    /*
                    String eop_reqUrl = new BaseBean().getPropValue("SW003", "eop_reqUrl");
                    if (eop_reqUrl.isEmpty()) {
                        eop_reqUrl = "https://testeop.macaowater.com";
                    }
                    String eop_reqToken = new BaseBean().getPropValue("SW003", "eop_reqToken");
                    if (eop_reqToken.isEmpty()) {
                        eop_reqToken = "EapUfida20200630D1207F4TEST";
                    }
                    HttpRequest http = new HttpRequest();
                    JSONObject params = new JSONObject();
                    params.put("reqNb", http.getTimestamp());
                    params.put("reqLang", "zh");
                    params.put("reqToken", eop_reqToken);
                    Map<String, Object> reqData = new HashMap<>();
                    Map<String, Object> billHead = new HashMap<>();
                    billHead.put("id", requestid);
                    billHead.put("cbilltypecode", "4D");
                    billHead.put("dbilldate",dateTools.getCurrentDate());
                    billHead.put("pk_corp", "MW");
                    billHead.put("vbillcode", requestid);
                    billHead.put("cdispatcherid", "21");
                    billHead.put("cdeptid", dbTools.getYyDepCode(lastoperator));
                    billHead.put("pk_calbody", "MW");
                    billHead.put("cwarehouseid", cwarehouseid);//倉庫
                    //billHead.put("coperatorid", dbTools.getWorkCode(creater + ""));
                    billHead.put("coperatorid",dbTools.getWorkCode(lastoperator));
                    billHead.put("fbillflag", "2");
                    billHead.put("vnote", "");
                    billHead.put("vuserdef20", main.get("docno"));
                    //添加billHead至reqData
                    reqData.put("billHead", billHead);
                    RecordSet rs3 = new RecordSet();
                    List<Map<String, Object>> billBodys = new ArrayList<>();
                    rs1.execute("select sum(applicationQuantity)as applicationQuantity,assetCode,projectcode from " + tableName + "_dt1 where mainid=" + billid+" group by assetCode,projectcode");
                    while (rs1.next()) {
                        Map<String, Object> billBody = new HashMap<>();
                        String assetCode = dbTools.getAssetCode(rs3, rs1.getString("assetCode"));
                        billBody.put("cinventoryid", assetCode);
                        String cprojectid = rs1.getString("projectcode");
                        billBody.put("cprojectid", cprojectid);
                        billBody.put("dbizdate", dateTools.getCurrentDate());
                        Double noutnum = rs1.getDouble("applicationQuantity");
                        Double nprice = 0.0;
                        Double nmny = 0.0;
                        billBody.put("noutnum", 0 - noutnum);
                        billBody.put("nprice", 0.00);
                        billBody.put("nmny", 0.00);
                        billBodys.add(billBody);
                    }
                    */
                    rs2.execute("select sum(applicationQuantity)as applicationQuantity,assetCode,field1,userid from " + tableName + "_dt1 where mainid=" + billid+" group by assetCode,field1,userid");
                    while (rs2.next()){
                        //String assetCode = dbTools.getAssetCode(rs3, rs2.getString("assetCode"));
                        Map<String,String> assetInfo =  dbTools.getAssetAllFieldoByID(rs3, rs2.getString("assetCode"));
                        Double noutnum = rs2.getDouble("applicationQuantity");
                        Map<String, String> logMap = new HashMap();
                        logMap.put("assetCode",assetInfo.get("assetcode"));
                        logMap.put("assetName", assetInfo.get("assetname"));
                        logMap.put("quantity", noutnum+"");
                        logMap.put("requestNumber", requestid);
                        logMap.put("reason", "OA退庫");
                        String field1 = rs2.getString("field1");
                        //String field2 = rs2.getString("field2");
                        logMap.put("field1", field1);
                        logMap.put("field2", "");
                        logMap.put("userid", rs2.getString("userid"));
                        logList.add(logMap);
                    }
                    /*
                    //添加billBody至reqData
                    reqData.put("billBody", billBodys);
                    params.put("reqData", reqData);
                    log.writeLog("---傳入用友參數如下---");
                    log.writeLog(JSON.toJSONString(params));
                    JSONObject json = http.CallApiPost(eop_reqUrl + "/eop/eapToUfida/icBillUpdate", JSONObject.toJSONString(params));
                    log.writeLog("---傳入用友結果如下---");
                    log.writeLog(JSON.toJSONString(json));
                    loggingDao.wrtieLog("0", "/eop/eapToUfida/icBillUpdate", JSON.toJSONString(params), JSON.toJSONString(json));
                    //对接用友
                    if(json==null || !json.getString("state").equals("0")){
                        rst2.rollback();
						requestInfo.getRequestManager().setMessageid(requestid);
						requestInfo.getRequestManager().setMessagecontent("物料歸還傳入用友失敗;\n"+JSON.toJSONString(json));
						return FAILURE_AND_CONTINUE;
					}else{
                        try {
                            for (int i = 0; i < logList.size(); i++) {
                                Map<String, String> log = logList.get(i);
                                loggingDao.wrtieInventoryLog(rst2,"0", log.get("assetCode"), Double.parseDouble(log.get("quantity")),
                                        log.get("requestNumber"), log.get("reason"),
                                        log.get("field1"), log.get("field2"), "5",
                                        log.get("userid"),operator,log.get("assetName"),"","");
                            }
                        }catch (Exception e){
                            rst2.rollback();
                            e.printStackTrace();
                            requestInfo.getRequestManager().setMessageid(requestid);
                            requestInfo.getRequestManager().setMessagecontent("記錄歸還日志異常;\n"+e.getMessage());
                            return FAILURE_AND_CONTINUE;
                        }

                        rst2.commit();//提交OA的庫存更新
					}
                    */

                    try {
                        for (int i = 0; i < logList.size(); i++) {
                            Map<String, String> log = logList.get(i);
                            loggingDao.wrtieInventoryLog(rst2,"0", log.get("assetCode"), Double.parseDouble(log.get("quantity")),
                                    log.get("requestNumber"), log.get("reason"),
                                    log.get("field1"), log.get("field2"), "5",
                                    log.get("userid"),operator,log.get("assetName"),"","");
                        }
                    }catch (Exception e){
                        rst2.rollback();
                        e.printStackTrace();
                        requestInfo.getRequestManager().setMessageid(requestid);
                        requestInfo.getRequestManager().setMessagecontent("記錄歸還日志異常;\n"+e.getMessage());
                        return FAILURE_AND_CONTINUE;
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                rst2.rollback();
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("歸檔更新數量失敗,程序異常;\n" + e.getMessage());
                return FAILURE_AND_CONTINUE;
            }


        }
        rst2.commit();//提交OA的庫存更新
        return SUCCESS;
    }

}
